package org.jeecg.modules.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.ptUsers.entity.PtUsers;
import org.jeecg.modules.ptUsers.service.IPtUsersService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * app对外全部接口
 */
@Slf4j
@RestController
@RequestMapping("/app/api")
public class AppUserinfoApiController {

    @Autowired
    private IPtUsersService ptUsersService;

    /**
     * 用户信息表
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("用户信息")
    @PostMapping(value = "/user_info")
    public Result<PtUsers> user_info(@RequestBody JSONObject jsonObject) {
        String time = jsonObject.getString("time");
        String token= jsonObject.getString("token");
        String md5Str = DigestUtils.md5DigestAsHex("prescription".getBytes());
        String token1=DigestUtils.md5DigestAsHex((md5Str+time).getBytes());
        String userId=jsonObject.getString("userId");
        if(token.equals(token1)){
            PtUsers user=ptUsersService.getById(userId);
            PtUsers sysUser = new PtUsers();
            sysUser.setId(userId);
            sysUser.setPhone(user.getPhone());
            sysUser.setAvatar(user.getAvatar());
            sysUser.setSex(user.getSex());
            sysUser.setNickname(user.getNickname());
            sysUser.setRealname(user.getRealname());
            sysUser.setEmail(user.getEmail());
            sysUser.setBirthday(user.getBirthday());
            sysUser.setUserName(user.getUserName());
            sysUser.setIsDoctor(user.getIsDoctor());
            sysUser.setUserState(user.getUserState());
            return Result.OK(sysUser);
        }else {
            return Result.OK("token验证失效");
        }

    }

    /**
     * 修改用户信息
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "修改用户-编辑")
    @ApiOperation(value="修改用户-编辑", notes="修改用户-编辑")
    @RequestMapping(value = "/edit_user")
    public Result<String> edit(@RequestBody JSONObject jsonObject){
        String time = jsonObject.getString("time");
        String token= jsonObject.getString("token");
        String md5Str = DigestUtils.md5DigestAsHex("prescription".getBytes());
        String token1=DigestUtils.md5DigestAsHex((md5Str+time).getBytes());
        if(token.equals(token1)){
            String userId=jsonObject.getString("userId");
            String sex= jsonObject.getString("sex");
            String nickname= jsonObject.getString("nickname");
            String avatar=jsonObject.getString("avatar");
            String email=jsonObject.getString("email");
            String realname=jsonObject.getString("realname");
            Date birthday=jsonObject.getDate("birthday");
            //修改用户基本信息
            QueryWrapper<PtUsers> sysUserQueryWrapper = new QueryWrapper<>();
            sysUserQueryWrapper.eq("id",userId);
            PtUsers sysUser=ptUsersService.getOne(sysUserQueryWrapper);
            sysUser.setNickname(nickname);
            sysUser.setSex(String.valueOf(sex));
            sysUser.setAvatar(avatar);
            sysUser.setEmail(email);
            sysUser.setRealname(realname);
            sysUser.setBirthday(birthday);
            sysUser.setUpdateBy(sysUser.getUserName());
            ptUsersService.updateById(sysUser);
            return Result.OK("编辑成功!");
        }else {
            return Result.OK("token验证失效");
        }
    }

    /**
     * 修改用户手机号
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "修改用户手机号-编辑")
    @ApiOperation(value="修改用户手机号-编辑", notes="修改用户手机号-编辑")
    @RequestMapping(value = "/edit_phone")
    public Result<JSONObject> edit_phone(@RequestBody JSONObject jsonObject){
        Result<JSONObject> result = new Result<JSONObject>();
        String time = jsonObject.getString("time");
        String token= jsonObject.getString("token");
        String md5Str = DigestUtils.md5DigestAsHex("prescription".getBytes());
        String token1=DigestUtils.md5DigestAsHex((md5Str+time).getBytes());
        if(token.equals(token1)){
            String userId= jsonObject.getString("userId");
            String phone= jsonObject.getString("phone");
            String newphone= jsonObject.getString("newphone");
            String smscode=jsonObject.getString("smscode");

            QueryWrapper<PtUsers> sysUserQueryWrapper = new QueryWrapper<>();
            sysUserQueryWrapper.eq("id",userId);
            PtUsers sysUser=ptUsersService.getOne(sysUserQueryWrapper);
            //修改用户手机号
            //        String redisKey = CommonConstant.PHONE_REDIS_KEY_PRE + phone;
            //        Object code = redisUtil.get(redisKey);
            //           if (code == null) {
//            result.setMessage("手机验证码失效，请重新获取");
//            result.setSuccess(false);
//            return result;
//        }
            if(!sysUser.getPhone().equals(phone)){
                result.setMessage("手机号码不正确");
                result.setSuccess(false);
                return result;
            }
            //  if (!smscode.equals(code.toString())) {
            if (!smscode.equals("123456")) {
                result.setMessage("手机验证码错误");
                result.setSuccess(false);
                return result;
            }

            sysUser.setPhone(newphone);
            ptUsersService.updateById(sysUser);
            return Result.OK("编辑成功!");
        }else {
            return Result.OK("token验证失效");
        }
    }
}
